package com.leslie.hhcommunity.service.impl;

import com.leslie.hhcommunity.dao.MessageMapper;
import com.leslie.hhcommunity.entity.Message;
import com.leslie.hhcommunity.service.MessageService;
import com.leslie.hhcommunity.util.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

/**
 * @author
 * @PackageName:com.leslie.hhcommunity.service.impl
 * @ClassnName:MessageServiceImpl
 * @Description:
 * @Date 2022/1/4  20:48
 */
@Service
public class MessageServiceImpl implements MessageService {

	@Autowired
	MessageMapper messageMapper;
	@Autowired
	SensitiveFilter sensitiveFilter;

	/**
	 * 查询当前用户的会话列表，针对每个会话只返回一条最新的私信
	 *
	 * @param userId
	 * @param offset
	 * @param limit
	 * @return
	 */
	@Override
	public List<Message> selectConversations(int userId, int offset, int limit) {
		return messageMapper.selectConversations(userId, offset, limit);
	}

	/**
	 * 查询当前用户的会话数量
	 *
	 * @param userId
	 * @return
	 */
	@Override
	public int selectConversationCount(int userId) {
		return messageMapper.selectConversationCount(userId);
	}

	/**
	 * 查询某个会话所包含的私信列表
	 *
	 * @param conversationId
	 * @param offset
	 * @param limit
	 * @return
	 */
	@Override
	public List<Message> selectLetters(String conversationId, int offset, int limit) {
		return messageMapper.selectLetters(conversationId, offset, limit);
	}

	/**
	 * 查询某个会话包含的私信数量
	 *
	 * @param conversationId
	 * @return
	 */
	@Override
	public int selectLetterCount(String conversationId) {
		return messageMapper.selectLetterCount(conversationId);
	}

	/**
	 * 查询未读私信数量,conversationId根据有无 动态拼接
	 *
	 * @param userId
	 * @param conversationId
	 * @return
	 */
	@Override
	public int selectLetterUnreadCount(int userId, String conversationId) {
		return messageMapper.selectLetterUnreadCount(userId, conversationId);
	}

	/**
	 * 添加私信
	 * @param message
	 * @return
	 */
	@Override
	public int insertMessage(Message message) {
		message.setContent(HtmlUtils.htmlEscape(message.getContent()));
		message.setContent(sensitiveFilter.filter(message.getContent()));
		return messageMapper.insertMessage(message);
	}

	/**
	 * 读到消息后更新为已读
	 * @param ids
	 * @param status
	 * @return
	 */
	@Override
	public int updateStatus(List<Integer> ids, int status) {
		return messageMapper.updateStatus(ids, status);
	}

	/**
	 * 查询某个主题下最新的通知
	 * @param userId
	 * @param topic
	 * @return
	 */
	@Override
	public Message selectLatesNotice(int userId, String topic) {
		return messageMapper.selectLatesNotice(userId, topic);
	}

	/**
	 * 查询某个主题下通知数量
	 * @param userId
	 * @param topic
	 * @return
	 */
	@Override
	public int selectNoticeCount(int userId, String topic) {
		return messageMapper.selectNoticeCount(userId, topic);
	}

	/**
	 * 查询未读的通知的数量
	 * @param userId
	 * @param topic
	 * @return
	 */
	@Override
	public int selectNoticeUnreadCount(int userId, String topic) {
		return messageMapper.selectNoticeUnreadCount(userId, topic);
	}

	/**
	 * 查看系统通知
	 * @param userId
	 * @param topic
	 * @param offset
	 * @param limit
	 * @return
	 */
	@Override
	public List<Message> selectNotices(int userId, String topic, int offset, int limit) {
		return messageMapper.selectNotices(userId, topic, offset, limit);
	}
}
